/**
 * Setting模型 - 用于存储系统设置信息
 */
const mongoose = require('mongoose');

const SettingSchema = new mongoose.Schema({
  key: { 
    type: String, 
    required: true, 
    unique: true 
  },
  value: { 
    type: String, 
    required: true 
  },
  createdAt: { 
    type: Date, 
    default: Date.now 
  },
  updatedAt: { 
    type: Date, 
    default: Date.now 
  }
});

// 更新前自动更新updatedAt字段
SettingSchema.pre('findOneAndUpdate', function() {
  this.set({ updatedAt: new Date() });
});

// 创建静态方法模拟Sequelize的upsert方法
SettingSchema.statics.upsert = async function(data) {
  return this.findOneAndUpdate(
    { key: data.key }, 
    data, 
    { new: true, upsert: true }
  );
};

module.exports = mongoose.model('Setting', SettingSchema); 